package components.google
{
	import UIFree.controls.uif_label;
	import UIFree.uif_component;
	import UIFree.uif_loader;
	
	import components.clt_component;
	
	import flash.display.BlendMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.BlurFilter;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	
	public class clt_searchItem extends clt_component {
		private var _image: uif_loader;
		private var _shining: Number;
		private var _info: uif_label;
		public function clt_searchItem(parent:uif_component, a_data: Object, ax:int=0, ay:int=0, awidth:int=0, aheight:int=0, styleClass:Object='') {
			super(parent, ax, ay, awidth, aheight, styleClass);
			_data = a_data;
			_shining = 0;
		}
		
		override protected function createComponents():void {
			var rect: Rectangle = getInner();
			var gfSize: Number = Math.min(width / 5, height / 5);
			
			_image = new uif_loader(this, '', doCompleteImage, rect.x, rect.y);
			_image.filters = [
				new GlowFilter(0xFFFF00, 0, gfSize, gfSize, 1, 2, true),
				new BlurFilter(0, 0, 2)
			]			
			rect.top += rect.height - 15;
			_info = new uif_label(this, rect.x, rect.y, rect.width, rect.height, 'searchItemInfo');
		}
		
		override protected function listenEvents():void {
			super.listenEvents();
			addEvent(MouseEvent.ROLL_OVER, doRollOver);
			addEvent(MouseEvent.ROLL_OUT, doRollOut);
		}
		
		private function doRollOver(e: Event): void {
			fx('shining', shining, 1).start();
		}
		
		private function doRollOut(e: Event): void {
			fx('shining', shining, 0).start();
		}
		
		override protected function setData(value:Object):void {
			super.setData(value);
			if (value && value.link && value.image) {
				_image.load(new URLRequest(value.image.thumbnailLink));
				if (value.image.byteSize)
					_info.text = Math.round(value.image.byteSize / 1024) + 'Кб ' +
								value.image.width + 'x' + value.image.height;
			}
		}
		
		protected function doCompleteImage(e: Event): void {
			var size: Point = new Point(_image.contentLoaderInfo.width, _image.contentLoaderInfo.height);
			var rect: Rectangle = EnterHere(size);
			_image.x = rect.x;
			_image.y = rect.y;
			_image.width = rect.width;
			_image.height = rect.height;
		}
		
		public function get shining(): Number {
			return _shining;
		}
		
		public function set shining(a_value: Number): void {
			_shining = a_value;
			if (_image) {
				var a_filters: Array = _image.filters;
				a_filters[0].alpha = _shining / 1.1;
				a_filters[1].blurX = (Math.cos(_shining * Math.PI * 2 + Math.PI) + 1) * 3;
				a_filters[1].blurY = a_filters[1].blurX;
				_image.filters = a_filters;
			}
		}
	}
}